﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using Document.Entities;

namespace Document.Providers
{
    internal class PracticeSpecialistProvider
    {
        private readonly OleDbConnection data;
        private int psp_StudentGroup = 2;

        //Practice Specialist
        private string sp_BookName = "шаблон приказа_прак_специалисты";
        private int sp_Fio = 1;
        private int sp_Manager = 4;
        private int sp_Note = 7;
        private int sp_PracticeCompany = 3;
        private int sp_Teacher = 6;
        private int sp_Theme = 5;

        public PracticeSpecialistProvider(string filePath)
        {
            if (String.IsNullOrEmpty(filePath)) return;
            //Configurate connet to excel file
            string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" +
                                       "Extended Properties=Excel 8.0;";
            data = new OleDbConnection(sConnectionString);
        }

        public List<StudentInfo> Load()
        {
            data.Open();
            var objCmdSelect = new OleDbCommand("SELECT * FROM [" + sp_BookName + "$]", data);
            OleDbDataReader reader = objCmdSelect.ExecuteReader();
            //Init student list
            var orderList = new List<StudentInfo>();
            //Read error
            if (reader == null) return null;

            while (reader.Read())
            {
                string[] policy = reader.GetValue(sp_Fio).ToString().Split(' ');
                if (policy.Count() != 3) continue;
                var student = new StudentInfo
                                  {
                                      Fio = reader.GetValue(sp_Fio).ToString().Trim(),
                                      StudentGroup = reader.GetValue(psp_StudentGroup).ToString().Trim(),
                                      Theme = reader.GetValue(sp_Theme).ToString(),
                                      Teacher = reader.GetValue(sp_Teacher).ToString().Trim(),
                                      PracticeCompany = reader.GetValue(sp_PracticeCompany).ToString().Trim(),
                                      Manager = reader.GetValue(sp_Manager).ToString().Trim(),
                                      Note = reader.GetValue(sp_Note).ToString().Trim()
                                  };

                orderList.Add(student);
            }

            data.Close();

            return orderList.Count != 0 ? orderList : null;
        }
    }
}